package cn.coderliu.voteweb.common.exception;

import cn.coderliu.voteweb.common.util.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author liu
 * 异常处理类
 */
@ControllerAdvice
@Slf4j
public class ExceptionHandle {
    @ResponseBody
    @ExceptionHandler({WebException.class})
    public Object handleException(WebException e) {
        //异常输出开始（运行将此代码注释）
        e.printStackTrace();
        //结束
        log.error(ExceptionUtils.getStackTrace(e));
        return R.error(e.getCode(), e.getMessage());
    }
}
